package ui 
{
	import com.dozen.mc.DUIBackground;
	import common.event.DUIEventer;
	import common.util.MapBoxUtil;
	import ui.fl.DUIBackgroundEx;
	import ui.fl.DUIBuildingEx;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import vo.LocationVO;
	/**
	 * ...
	 * @author Surui
	 */
	public class UIMap extends Sprite
	{
		/** 鼠标点击坐标 */
		private var m_mouseClickPoint:Point = new Point();
		/** 建筑原坐标 */
		private var m_buildingPoint:Point = new Point();
		/** 鼠标是否按下 */
		private var m_mouseDown:Boolean = false;
		/** 选中的建筑 */
		private var m_selectedBuilding:DUIBuildingEx;
		
		/** 建筑层 */
		public var m_houseLayer:UIHouseLayer = new UIHouseLayer();
		private var m_xx:int;
		private var m_yy:int;
		public function UIMap() 
		{
			initEvent();
		}
		public function init(row:int, cos:int):void {
			m_houseLayer.mouseEnabled = false;
			m_houseLayer.x = 500;
			m_houseLayer.y = 100;
			this.addChild( m_houseLayer );
			this.m_houseLayer.drawMap(row,cos);
		}
		/**
		 * 初始化监听器
		 */
		private function initEvent():void {
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		/**
		 * 鼠标按下
		 * @param	event
		 */
		private function onMouseDown( event:MouseEvent ):void {
			m_mouseClickPoint.x = event.stageX;
			m_mouseClickPoint.y = event.stageY;
			trace("鼠标点击对象：", event.target);
			//trace("鼠标点击坐标：", event.target.mouseX, event.target.mouseY);
			m_buildingPoint.x = event.target.x;
			m_buildingPoint.y = event.target.y;
			//trace("物品初始坐标：", event.target.x, event.target.y);
			m_mouseDown = true;
			trace("启动鼠标编辑");
		}
		/**
		 * 鼠标移动
		 * @param	event
		 */
		private function onMouseMove( event:MouseEvent ):void {
			var stageX:Number = event.stageX;
			var stageY:Number = event.stageY;
			//trace("鼠标移动坐标：", event.stageX, event.stageY);
			//trace(m_mouseDown, event.target);
			//trace("移动地图参数", m_mouseDown, event.target);
			//trace("移动建筑参数", UIFade.self.editMode, m_selectedBuilding);
			if ( m_mouseDown && event.target is UIMap ) {
				//trace("移动地图");
				var map:UIMap = event.target as UIMap;
				map.xx = m_buildingPoint.x + ( stageX - m_mouseClickPoint.x );
				map.yy = m_buildingPoint.y + ( stageY - m_mouseClickPoint.y );
				//trace("物品移动坐标：", event.target.x, event.target.y);
			} else if( UIFade.self.editMode && m_selectedBuilding ){
				//trace("移动建筑");
				var building:DUIBuildingEx = event.target as DUIBuildingEx;
				var realPoint:Point = new Point(m_buildingPoint.x + ( stageX - m_mouseClickPoint.x ), m_buildingPoint.y + ( stageY - m_mouseClickPoint.y ));
				//var boxCenterPoint:Point = m_houseLayer.getBoxCenterPoint(realPoint);
				var boxPoint:Point = MapBoxUtil.getBoxPoint( realPoint );
				var boxCenterPoint:Point = MapBoxUtil.getRealPoint( boxPoint.x, boxPoint.y );
				// 更新物品坐标
				m_selectedBuilding.x = boxCenterPoint.x;
				m_selectedBuilding.y = boxCenterPoint.y;
				// 发出检测当前坐标是否可以摆放事件
				DUIEventer.dispatch(DUIEventer.TRY_MOVE_MAP_OBJECT_TO, new LocationVO(boxPoint.x, boxPoint.y));
			}
		}

		/**
		 * 鼠标弹起
		 * @param	event
		 */
		private function onMouseUp( event:MouseEvent ):void {
			m_mouseDown = false;
			trace( "终止鼠标编辑" );
			trace( "m_selectedBuilding:" + m_selectedBuilding );
			// 如果当前是编辑模式
			if ( UIFade.self.editMode ) {
				if ( !m_selectedBuilding && event.target is DUIBuildingEx) {
					trace(" 设置移动对象 ");
					m_selectedBuilding = event.target as DUIBuildingEx;
					DUIEventer.dispatch(DUIEventer.TRY_SELECT_MAP_OBJECT, m_selectedBuilding.itemId);
				} else if( m_selectedBuilding ){
					trace(" 清除移动对象 ");
					//m_selectedBuilding = null;
					var boxPoint:Point = MapBoxUtil.getBoxPoint(new Point(m_selectedBuilding.x, m_selectedBuilding.y));
					DUIEventer.dispatch(DUIEventer.TRY_PLACE_MAP_OBJECT_AT, new LocationVO(boxPoint.x, boxPoint.y));
				}
			}
		}
		/**
		 * 添加建筑
		 * @param	building
		 */
		public function addBuilding( building:DUIBuildingEx ):void {
			this.m_houseLayer.addChild(building);
		}
		
		public function get xx():int 
		{
			return m_xx;
		}
		
		public function set xx(value:int):void 
		{
			m_xx = value;
		}
		
		public function get yy():int 
		{
			return m_yy;
		}
		
		public function set yy(value:int):void 
		{
			m_yy = value;
		}
		
		public function get selectedBuilding():DUIBuildingEx 
		{
			return m_selectedBuilding;
		}
		
		public function set selectedBuilding(value:DUIBuildingEx):void 
		{
			m_selectedBuilding = value;
		}
	}

}